package com.bit.demo;

import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;


//@Controller
//@ResponseBody
@RestController
@RequestMapping("/request")
public class RequestController {

    @RequestMapping("/login")
    public String login(String name){
        return "接收到参数 name:" + name;
    }

    @RequestMapping("/r1")
    public String r1(Integer age){
        return "接收到参数 age:" + age;
    }

    @RequestMapping("/r2")
    public String r2(int age){
        return "接收到参数 age:" + age;
    }

    @RequestMapping("/r3")
    public String r3(String name, String password){
        return "接收到参数 name:" + name + ", password:"+ password;
    }

    @RequestMapping("/r4")
    public String r4(Person person){
        return "接收到参数 person:" + person;
    }

    @RequestMapping("/r5")
    public String r5(@RequestParam(value = "q", required = false) String keyword){
        return "搜索词为 :" + keyword;
    }

    @RequestMapping("/r6")
    public String r6(String[] arr){
        return "数组:" + Arrays.toString(arr) + ", length:" + arr.length;
    }

    @RequestMapping("/r7")
    public String r7(@RequestParam(required = false) List<String> list){
        StringBuilder builder = new StringBuilder();
        for (String s: list){
            builder.append(s).append(",");
        }
        return "集合:" + builder.toString();
    }

    @RequestMapping("/r8")
    public String r8(@RequestBody Person person){
        return "接收json数据:" + person;
    }

    @RequestMapping("/article/{articleId}")
    public String r9(@PathVariable("articleId") Integer articleId){
        return "r9 文章ID:" + articleId;
    }

    @RequestMapping("/article/{articleId}/{type}")
    public String r10(@PathVariable("articleId") Integer articleId, @PathVariable String type){
        return "r10 文章ID:" + articleId + ", type:" + type;
    }

    @RequestMapping("/r11")
    public String r11(@RequestPart("file111") MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();
        File destFile = new File("D:\\temp\\"+ originalFilename);
        //上传文件
        file.transferTo(destFile);
        return "存储文件成功";
    }

    @RequestMapping("/r12")
    public String r12(@RequestParam("file111") MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();
        File destFile = new File("D:\\temp\\"+ originalFilename);
        //上传文件
        file.transferTo(destFile);
        return "存储文件成功";
    }

    @RequestMapping("/getCookie")
    public String getCookie(HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        if (cookies!=null) {
            for (Cookie cookie: cookies){
                System.out.println(cookie.getName() + ":" + cookie.getValue());
            }
        }
        return "获取cookie成功";
    }

    @RequestMapping("/getCookie2")
    public String getCookie2(@CookieValue("name") String name){
        return "获取cookie成功" + name;
    }


    @RequestMapping("/setSession")
    public String setSession(String name, HttpServletRequest request){
        HttpSession session = request.getSession();
        session.setAttribute("name", name);
        session.setAttribute("age", 1);
        return "获取session成功";
    }

    @RequestMapping("/setSession2")
    public String setSession2(String name, HttpSession session){
//        HttpSession session = request.getSession();
        session.setAttribute("name", name);
        session.setAttribute("age", 1);
        return "获取session成功";
    }

    @RequestMapping("/getSession")
    public String getSession(HttpServletRequest request){
        HttpSession session = request.getSession(false);
        String name = (String)session.getAttribute("name");
        Integer age = (Integer) session.getAttribute("age");
        return "从session中获取信息, name:" +name + ", age:" + age;
    }

    @RequestMapping("/getSession2")
    public String getSession2(HttpSession session){
        String name = (String)session.getAttribute("name");
        Integer age = (Integer) session.getAttribute("age");
        return "从session中获取信息, name:" +name + ", age:" + age;
    }

    @RequestMapping("/getSession3")
    public String getSession3(@SessionAttribute("name") String name){
        return "从session中获取信息, name:" +name;
    }

    @RequestMapping("/getHeader")
    public String getHeader(HttpServletRequest request){
        String userAgent = request.getHeader("User-Agent");
        return "从header中获取信息, userAgent:" +userAgent;
    }

    @RequestMapping("/getHeader2")
    public String getHeader2(@RequestHeader("User-Agent") String userAgent){
        return "从header中获取信息, userAgent:" +userAgent;
    }


}
